package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.InvoicePurMapper;
import com.ruoyi.crm.domain.InvoicePur;
import com.ruoyi.crm.service.IInvoicePurService;

/**
 * 采购发票Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-01
 */
@Service
public class InvoicePurServiceImpl implements IInvoicePurService
{
    @Autowired
    private InvoicePurMapper invoicePurMapper;

    /**
     * 查询采购发票
     *
     * @param id 采购发票主键
     * @return 采购发票
     */
    @Override
    public InvoicePur selectInvoicePurById(String id)
    {
        return invoicePurMapper.selectInvoicePurById(id);
    }

    /**
     * 查询采购发票列表
     *
     * @param invoicePur 采购发票
     * @return 采购发票
     */
    @Override
    public List<InvoicePur> selectInvoicePurList(InvoicePur invoicePur)
    {
        return invoicePurMapper.selectInvoicePurList(invoicePur);
    }

    /**
     * 新增采购发票
     *
     * @param invoicePur 采购发票
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInvoicePur(InvoicePur invoicePur)
    {
        int rows = invoicePurMapper.insertInvoicePur(invoicePur);
        insertCustomerProduct(invoicePur);
        return rows;
    }

    /**
     * 修改采购发票
     *
     * @param invoicePur 采购发票
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInvoicePur(InvoicePur invoicePur)
    {
        invoicePur.setUpdateTime(DateUtils.getNowDate());
        invoicePurMapper.deleteCustomerProductByCustomerName(invoicePur.getId());
        insertCustomerProduct(invoicePur);
        return invoicePurMapper.updateInvoicePur(invoicePur);
    }

    /**
     * 批量删除采购发票
     *
     * @param ids 需要删除的采购发票主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInvoicePurByIds(String[] ids)
    {
        return invoicePurMapper.deleteInvoicePurByIds(ids);
    }

    /**
     * 删除采购发票信息
     *
     * @param id 采购发票主键
     * @return 结果
     */
    @Override
    public int deleteInvoicePurById(String id)
    {
        invoicePurMapper.deleteCustomerProductByCustomerName(id);
        return invoicePurMapper.deleteInvoicePurById(id);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param invoicePur 采购发票对象
     */
    public void insertCustomerProduct(InvoicePur invoicePur)
    {
        List<CustomerProduct> customerProductList = invoicePur.getCustomerProductList();
        String id = invoicePur.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                invoicePurMapper.batchCustomerProduct(list);
            }
        }
    }
}